/* ****************************************************************************
prerequisites.h
-------------------------------------------------------------------------------

Copyright (c) 2017, Tain L.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**************************************************************************** */

#if !defined(SPEARS_PREREQUISITES_H)
#define SPEARS_PREREQUISITES_H

#include <assert.h>
#include <memory>
#include <cstring>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <unordered_map>
#include <string>
#include <sstream>
#include <exception>
#include <thread>
#include <mutex>
#include <condition_variable>

#ifdef _DEBUG
#	define	DEBUG_PLUG(plug)	{plug;}
#else
#	define	DEBUG_PLUG(plug)
#endif

#if DLL_DEV
#	define SPEARS_API	__declspec(dllexport)
#else
#	define SPEARS_API	__declspec(dllimport)
#endif

#ifndef _HUGE_ENUF
#define _HUGE_ENUF  1e+300  // _HUGE_ENUF*_HUGE_ENUF must overflow
#endif

#ifndef INFINITY
#define INFINITY   ((float)(_HUGE_ENUF * _HUGE_ENUF))
#endif

#define INVALID_INDEX	((size_t)(-1))

#define BEGIN_TRY()					try{
#define CATCH_AND_THROW_ALL()		}catch(...){throw;}

namespace Spears
{

	typedef unsigned char		byte;
	typedef int					int32;
	typedef long long			int64;
	typedef unsigned int		uint32;
	typedef unsigned long long	uint64;
	typedef void				*pvoid;
	typedef size_t				index;
	typedef size_t				uintx;

	template<typename _Type>
	inline void safe_delete(_Type*& p_target)
	{
		if (p_target != nullptr)
		{
			delete p_target;
			p_target = nullptr;
		}
	}

	template<typename _Type>
	inline void safe_delete_arr(_Type*& p_target)
	{
		if (p_target != nullptr)
		{
			delete[] p_target;
			p_target = nullptr;
		}
	}

	template<typename _Type>
	inline void safe_release(_Type*& p_target)
	{
		if (p_target != nullptr)
		{
			p_target->release();
			p_target = nullptr;
		}
	}

	template<typename _Type>
	inline void safe_delete(std::unique_ptr<_Type>& p_target)
	{
		if (nullptr != p_target)
		{
			delete p_target.release();
		}
	}
	
	template<typename _Type>
	inline void safe_delete_arr(std::unique_ptr<_Type>& p_target)
	{
		if (nullptr != p_target)
		{
			delete[] p_target.release();
		}
	}


	template<typename _Type>
	inline void safe_release(std::unique_ptr<_Type>& p_target)
	{
		if (nullptr != p_target)
		{
			p_target->release();
			p_target.release();
		}
	}
} //namespace Spears

#endif // SPEARS_PREREQUISITES_H